import http from "@/utils/http";
import * as types from '../actionType'

export const setChannels = (payload) => ({ type: types.SET_CHANNELS, payload })

export const setArticle = (payload) => ({ type: types.SET_ARTICLES, payload })

export const getChannels = () => {
    return async (dispatch) => {
        const { data: { channels } } = await http.get("/v1_0/channels")
        // console.log(channels);
        dispatch(setChannels(channels))
    }
}

export const getArticles = (params) => {
    return async (dispatch) => {
        const { data } = await http.get('/v1_0/mp/articles', {
            params
        })
        // console.log(data);
        dispatch(setArticle(data))
    }
}

export const delArticle = (id) => {
    return async (dispatch) => {
        await http.delete(`/v1_0/mp/articles/${id}`)
    }
}

export const addArticle = (data) => {
    return async (dispatch) => {
        await http.post("/v1_0/mp/articles", data)
    }
}

export const editArticle = (data, draft = false) => {
    return async dispatch => {
        await http.put(`/v1_0/mp/articles/${data.id}?draft=${draft}`, data)
    }
}

export const getArticle = id => {
    return async dispatch => {
        const data = await http.get('/v1_0/mp/articles/' + id)
        return data
    }
}
